package com.cat.graphTheory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-the-number-of-complete-components/
 * @create 2025/9/18 22:46
 * @since JDK17
 */

public class Solution09 {
    List<Integer>[] g;
    boolean[] vis;
    int cnt, v;
    void dfs(int cur) {
        vis[cur] = true;
        v++;
        cnt += g[cur].size();
        for (int x : g[cur]) {
            if (!vis[x]) {
                dfs(x);
            }
        }
    }
    public int countCompleteComponents(int n, int[][] edges) {
        g = new List[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        vis = new boolean[n];
        int m = edges.length, ans = 0;
        for (int i = 0; i < m; i++) {
            int from = edges[i][0], to = edges[i][1];
            g[from].add(to);
            g[to].add(from);
        }

        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                v = cnt = 0;
                dfs(i);
                if (v * (v - 1) == cnt) {
                    ans++;
                }
            }
        }

        return ans;
    }
}
